package com.sky.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.domain.Notice;
import com.sky.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author 尹稳健~
 * @version 1.0
 */
@Api(tags = "通知模块")
@Controller
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @GetMapping("/notice")
    public Notice notice(){
        return new Notice();
    }

    // 查询所有，包含分页查询、模糊查询
    @ApiOperation("查询所有，包含模糊查询")
    @GetMapping("/list")
    public String findAll(@RequestParam(value = "content",defaultValue = "")String title,
                          @RequestParam(value = "pn",defaultValue = "1")Integer pn,
                          Model model){
        PageHelper.startPage(pn,5);
        List<Notice> noticeList = noticeService.findAll(title);
        PageInfo<Notice> pageInfo = new PageInfo<>(noticeList);
        model.addAttribute("pageInfo",pageInfo);
        return "notice/list";
    }

    // 跳转更新/添加页面
    @ApiOperation("跳转更新/添加页面")
    @GetMapping("/add")
    public String toAddPage(Integer id,Model model){
        if (id != null){
            // 更新操作 数据回显
            Notice notice = noticeService.findNoticeById(id);
            model.addAttribute("notice",notice);
            model.addAttribute("msg","1");
        }else{
            // 添加操作
            model.addAttribute("msg","0");
        }
        return "notice/add";
    }

    // 添加操作/更新操作
    @ApiOperation("添加操作/更新操作")
    @PostMapping("/add")
    public String add(Integer id,Notice notice){
        if (id != null){
            noticeService.update(notice);
        }else{
            noticeService.insert(notice);
        }
        return "redirect:/notice/list";
    }

    // 删除操作
    @ApiOperation("删除操作")
    @GetMapping("/delete")
    public String delete(Integer id){
        noticeService.delete(id);
        return "redirect:/notice/list";
    }

}
